//
//  GDMHomeStatusesCell.swift
//  SinaWeibo
//
//  Created by mac on 15/11/30.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit

import SnapKit

let StatusCellMargin: CGFloat = 12
let StatusCellImageWidth: CGFloat = 35

class GDMHomeStatusesCell: UITableViewCell {
    
    var topConstraint : Constraint?
    
    var homeStatuseModel : GDMHomeStatusModel? {
        
        didSet{
            
         originaView.homeStatuseModel = homeStatuseModel

            topConstraint?.uninstall()
            
            if homeStatuseModel?.retweeted_status != nil{
                
                retweetedView.retweetedStatuses = homeStatuseModel?.retweeted_status
                
//                现在显示转发微博
                retweetedView.hidden = false
                
//                更新约束
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    
                    self.topConstraint = make.top.equalTo(retweetedView.snp_bottom).constraint
                })
                
            } else {
                
//                隐藏转发微博
                retweetedView.hidden = true
                
//                没有配图的时候更新约束
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    
                    self.topConstraint = make.top.equalTo(originaView.snp_bottom).constraint
                    
                })
            }
            
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
//        添加一个view
        addSetupUI()
        
//        设置了之后在点击的时候就不会被选中了
        selectionStyle = .None
    }

    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
   private func addSetupUI() {
    
//    首先创建一个view,并且添加到这个cell的view中
    contentView.addSubview(originaView)
    
    contentView.addSubview(retweetedView)
    
    contentView.addSubview(bottomView)
    
   
    
//    设置自动布局
    originaView.snp_makeConstraints { (make) -> Void in
        
        make.left.top.right.equalTo(contentView).constraint
        
//        设置一下他的高
//        make.height.equalTo(50)
        
    }
    
    retweetedView.snp_makeConstraints { (make) -> Void in
        
        make.left.right.equalTo(contentView)
        make.top.equalTo(originaView.snp_bottom)
        
    }
    
    bottomView.snp_makeConstraints { (make) -> Void in
        
        make.left.right.equalTo(contentView)
//        每一次都要修改根据有没有配图的约束
      self.topConstraint = make.top.equalTo(retweetedView.snp_bottom).constraint
        make.height.equalTo(44)
    }
    contentView.snp_makeConstraints { (make) -> Void in
        make.left.top.right.equalTo(self)
        make.bottom.equalTo(bottomView.snp_bottom)
    }
    
    }
    
//    懒加载视图

    private lazy var originaView : GDMOriginalView = GDMOriginalView()
    private lazy var retweetedView : GDMStatusRetweetedView = GDMStatusRetweetedView()
    private lazy var bottomView : GDMStatuesCellBottomView = GDMStatuesCellBottomView()
}
